#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#include "timer_manager.h"
using namespace yx;

void timeline::TimerManager::update()
{
	if (_timers.empty())
	{
		return;
	}

	int64_t now = timeline::Timer::getNow();

	for (auto it = _timers.begin(); it != _timers.end();)
	{
		if (it->first > now)
		{
			return;
		}

		it->second.on_timer();

		timeline::Timer t = it->second;
		it = _timers.erase(it);

		if (t._repeat != 0)
		{
			auto new_it = _timers.insert(std::make_pair(t._time, t));
			if (it == _timers.end() || it->first > new_it->first)
			{
				it = new_it;
			}
		}
	}
}
